#ifndef ORACLEDAO_H
#define ORACLEDAO_H

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/qsqlerror.h>
#include <QMutex>
#include <QSettings>
#include"ComFun.h"
#include <QDateTime>
#include <qdebug.h>


class OracleDAO
{
public:

    bool GetZhulian(QString &szDbName,QMap<QString,QString> &mapZhulian);
    bool GetJinyue(QString &szDbName,QMap<QString,QString> &mapJinyue);
    //获取前一个交易日日期
    QString GetPreTradeDay(QString &szDbName);
    //获取所有合约的前一交易日的成交量
    bool    GetAllPreVolume(QString &szDbName,QString strPreTradeDay);
    //获取所有合约的前一交易日最后一根5分钟K线的信息
    bool    GetAllPre5MinKLine(QString &szDbName,QString strPreTradeDay);


    bool GetInstrumentCnt(QString &szDbName);
    bool DeleteOldInstruments(QString &szDbName);
    void UpdateInstruments(QStringList &szHqList,QString &szDbName);

    bool MatchThread(QString &szDbName,QMap<QString,int> &MapMatchThrd);
    //插入主连的1分钟K线
    void InsertZhulian1MinRec(QString InstrumentID, KLineType* Kline , QString date,QString time,QMap<QString,ContractType> &MapContractType);
    //插入主连的5分钟K线
    void InsertZhulian5MinRec(QString InstrumentID, KLineType* Kline , QString date,QString time,QMap<QString,ContractType> &MapContractType);

    //插入1分钟K线记录
    void Insert1MinRec(QString InstrumentID, KLineType* Kline , QString date,QString time);
    //插入5分钟K线记录
    void Insert5MinRec(QString InstrumentID, KLineType* Kline , QString date,QString time);

    //行情数据入库
    void UpdateHqInfo(QStringList &szHqList, QString &szDbName);
    void UpdateCTPMarketInfo(QStringList &szHqList,QString &szDbName);
    bool GetInstruments(QString &szDbName,std::vector<QString>& ContractCodes,QMap<QString,ContractType> &MapContractType);
    bool GetContractTypes(QString &szDbName,std::vector<QString>& ContractTypes);

    bool SaveDayKLineData(QString ContractName,QString date,QString &szDbName);
    bool GetTheMainContractCode(QString &szDbName,QString ContractType,QString date,QString &MainContractCode);
    bool GetTheJinyueContractCode(QString &szDbName,QString ContractType,QString &JinyueContractCode);


    bool InsertZhulianRec(QString &szDbName,QString zhulianCode,QString date,QString ContractCode);
    bool SaveZhulianDayKLineData(QString &szDbName,QString zhulianCode,QString date,QString ContractCode,QMap<QString,ContractType> &MapContractType);
    bool SaveZhulianTickData(QString &szDbName,QString zhulianCode,QString date,QString ContractCode);

    bool InsertJinyueRec(QString &szDbName,QString jinyueCode,QString date,QString ContractCode);
    bool SaveJinyueDayKLineData(QString &szDbName,QString jinyueCode,QString date,QString ContractCode);
    bool SaveJinyueTickData(QString &szDbName,QString jinyueCode,QString date,QString ContractCode);

    OracleDAO(QString &szConnName);
    ~OracleDAO();
private:
    bool dbChecked(QSqlQuery &query);

    QSqlDatabase m_OracleDB;

    QMap<QString,int> m_MapPreVolume;
    QMap<QString,KLineType> m_Pre5MinKLine;

    static QMutex m_staticMutex;
};

#endif // ORACLEDAO_H
